package element.synt;

import element.base.Synt;
import intermediate_representation.AssignRP;
import intermediate_representation.ExpRP;
import symbal.Symbal;
import symbal.Vtype;
import symbal.constsymbal.ConstInt;
import symbal.constsymbal.ConstInt1;
import symbal.constsymbal.ConstInt2;
import symbal.constsymbal.ConstSymbal;
import table.InterCodeTable;

//LVal → Ident {'[' Exp ']'}
public class LVal extends Synt {
    public String toFormat() {
        String ident = this.findFirst().getContent();
        String str = InterCodeTable.symbalTable.findSymbal(ident).getMipsName();
        int loop = (this.getNodes().size() - 1) / 3;
        if (loop == 1) {
            Symbal symbal = InterCodeTable.symbalTable.findSymbal(ident);
            if (symbal.getVtype().equals(Vtype.INT2)) {
                str += "@";
            }
            str += "[";
            Exp exp = (Exp) this.getNodes().get(2);
            exp.tointerRP();
            if (symbal.getVtype().equals(Vtype.INT2)) {
                String ori = InterCodeTable.nowVar;
                InterCodeTable.pushvar();
                ExpRP expRP = new ExpRP(ori, String.valueOf(symbal.getLen2()), "*", InterCodeTable.nowVar);
                InterCodeTable.addCode(expRP);
            }
            str += InterCodeTable.nowVar;
            str += "]";
        } else if (loop == 2) {
            int nowindex = InterCodeTable.regindex;
            str += "[";
            Exp exp = (Exp) this.getNodes().get(2);
            exp.tointerRP();
            String ind1 = InterCodeTable.nowVar;
            Symbal symbal = InterCodeTable.symbalTable.findSymbal(ident);
            int len2 = symbal.getLen2();
            InterCodeTable.pushvar();
            String tmp = InterCodeTable.nowVar;
            InterCodeTable.interCode.add(new ExpRP(ind1, String.valueOf(len2), "*", tmp));
            exp = (Exp) this.getNodes().get(5);
            exp.tointerRP();
            String ind2 = InterCodeTable.nowVar;
            InterCodeTable.interCode.add(new ExpRP(tmp, ind2, "+", tmp));
//            InterCodeTable.regindex = nowindex;
            str += tmp;
            str += "]";
//            InterCodeTable.interCode.add(new AssignRP(ind1, str));
//            str = ind1;
        }
        return str;
    }

    public int calcu() {
        String str = this.findFirst().getContent();
        ConstSymbal symbal = InterCodeTable.constTable.findConst(str);
        if (symbal instanceof ConstInt) {
            return ((ConstInt) symbal).getValue();
        } else if (symbal instanceof ConstInt1) {
            Exp de1 = (Exp) this.getNodes().get(2);
            int len1 = de1.calcu();
            return ((ConstInt1) symbal).getValue(len1);
        } else if (symbal instanceof ConstInt2){
            Exp de1 = (Exp) this.getNodes().get(2);
            Exp de2 = (Exp) this.getNodes().get(5);
            int len1 = de1.calcu();
            int len2 = de2.calcu();
            return ((ConstInt2) symbal).getValue(len1, len2);
        }
        return 0;
    }

    public boolean isConst() {
        String str = this.findFirst().getContent();
        if (InterCodeTable.isConst(str)) {
            Symbal symbal1 = InterCodeTable.symbalTable.findSymbal(str);
            Vtype vtype = symbal1.getVtype();
            return vtype.equals(Vtype.INT);
        } else {
            return false;
        }
    }
}
